package com.example.Controller;

import com.example.dao.RecordDao;
import com.example.dao.ToupiaoDao;
import com.example.pojo.Record;
import com.example.pojo.Toupiao;
import com.example.pojo.emp;
import com.example.pojo.group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.templateresolver.ITemplateResolver;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
public class adminController {
    @Autowired
    ToupiaoDao toupiaoDao;
    @Autowired
    RecordDao recordDao;
    @RequestMapping("/addBook")
    public String addBook(@RequestParam("id") String id,
                          @RequestParam("name") String name,
                          @RequestParam("number") String number,
                          @RequestParam("price") String price,
                          Model model){
        System.out.println(id);
        System.out.println(name);
        System.out.println(number);
        System.out.println(price);
        if (!id.equals("")&&!name.equals("")&&!price.equals("")){
            if (toupiaoDao.find(id)==null)
            {
                toupiaoDao.insert(new Toupiao(id, name, "0", Double.parseDouble(price), number));
            }
            else {
                model.addAttribute("msg",id+"编号的图书已存在，请重新输入");
            }
        }
        System.out.println(id);
        System.out.println(name);
        System.out.println(price);
        return "addBook";
    }
    @RequestMapping("/allBook")
    public String allBook(Model model){
        List<Toupiao> all = toupiaoDao.findAll();
        List <emp> emps=new ArrayList<>();
        List<group> group = recordDao.group();
        String [] stock=new String[all.size()];
        int n=0;
        for (Toupiao toupiao : all) {
            stock[n]=toupiao.getNumber();
            n++;
            emps.add(new emp(toupiao.getId(),toupiao.getName(),"0",toupiao.getPrice()));
        }
        String []nums=new String[group.size()];
        for(int i = 0; i < group.size();i++)
        {
            nums[i]=group.get(i).getSum();
        }
        int m=0;
        //
        for (emp emp : emps) {
            System.out.println(emp.toString());
            if (nums.length<=   m){
                emp.setSum(String.valueOf(Integer.parseInt(stock[m])));
                System.out.println();
            }
            else {
                emp.setSum(String.valueOf(Integer.parseInt(stock[m]) - Integer.parseInt(nums[m])));
                System.out.println(String.valueOf(Integer.parseInt(stock[m]) - Integer.parseInt(nums[m])));
                m++;
            }
        }
        Collections.sort(emps);
        model.addAttribute("emps",emps);
        return "allBook";
    }
    String id;
    @GetMapping("/allBook/{id}")
    public String toupdate(@PathVariable(value = "id") String id,Model model){
        this.id=id;
        Toupiao toupiao = toupiaoDao.find(id);
        model.addAttribute("emp",toupiao);
        return "update";
    }
    @PostMapping("update")
    public String update(Toupiao toupiao){
        toupiao.setPiao("0");
        System.out.println(toupiao.toString());
        System.out.println(id);
        Toupiao toupiao1 = toupiaoDao.find(toupiao.getId());
        toupiao.setNumber(toupiao1.getNumber());
        System.out.println(toupiao.toString());
        int update = toupiaoDao.update(toupiao);
        System.out.println(update);
        return "redirect:allBook";
    }
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") String id,Model model){
        int id1 = toupiaoDao.delete(id);
        System.out.println(id1);
        List<Toupiao> all = toupiaoDao.findAll();
        List <emp> emps=new ArrayList<>();
        List<group> group = recordDao.group();
        String [] stock=new String[all.size()];
        int n=0;
        for (Toupiao toupiao : all) {
            stock[n]=toupiao.getNumber();
            n++;
            emps.add(new emp(toupiao.getId(),toupiao.getName(),"0",toupiao.getPrice()));
        }
        String []nums=new String[group.size()];
        for(int i = 0; i < group.size();i++)
        {
            nums[i]=group.get(i).getSum();
        }
        int m=0;
        for (emp emp : emps) {
            System.out.println(emp.toString());
            if (nums.length<=   m){
                emp.setSum(String.valueOf(Integer.parseInt(stock[m])));
                System.out.println();
            }
            else {
                emp.setSum(String.valueOf(Integer.parseInt(stock[m]) - Integer.parseInt(nums[m])));
                System.out.println(String.valueOf(Integer.parseInt(stock[m]) - Integer.parseInt(nums[m])));
                m++;
            }
        }
        Collections.sort(emps);
        model.addAttribute("emps",emps);
        return "allBook";
    }
    @RequestMapping("change")
    public String change(Model model){
        List<Record> by = recordDao.findBy("1");
        for (Record record : by) {
            record.setKind("未归还");
        }
        model.addAttribute("emps",by);
        return "changeState";
    }
    @GetMapping("/change/{time}")
    public String ch(@PathVariable("time") String time,Model model){
        System.out.println(time);
        List<Record> by = recordDao.findBy("1");
        for (Record record : by) {
            record.setKind("未归还");
        }
        String useName;
        String bookName;
        String number;
        String kind;
        String t;
        String[] split = time.split("null");
        useName=split[0];
        bookName=split[1];
        number=split[2];
        kind="1";
        t=split[4];
        Record record = new Record(useName, bookName, number, kind, t);
        System.out.println(record.toString());
        int i = Integer.parseInt(number);
        Record rrcord = recordDao.findRrcord(useName,bookName,number,"1",t);
        int delete = recordDao.delete(rrcord);
        System.out.println(delete);
        System.out.println("删除"+delete);
        if ((i - 1)!=0) {
            int insert = recordDao.insert(new Record(useName, bookName, String.valueOf(i - 1), "1", t));
            System.out.println("添加" + delete);
        }
        int insert1 = recordDao.insert(new Record(useName, bookName, "1", "2", t));

        System.out.println("状态"+insert1);
        System.out.println(rrcord.toString());
        List<Record> by1 = recordDao.findBy("1");
        for (Record record1 : by1) {
            System.out.println(record1.toString());
        }
        model.addAttribute("emps",by1);
        return "changeState";
    }
}
